package com.hxkcloud.nurse.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.adapter.MonitorPatientAdapter;
import com.hxkcloud.nurse.beans.LineListBean;
import com.hxkcloud.nurse.beans.MonitorAdapterBean;
import com.hxkcloud.nurse.beans.MonitorKindBean;
import com.hxkcloud.nurse.beans.MyChartListBean;
import com.hxkcloud.nurse.beans.resultbeans.ObjectResultBean;
import com.hxkcloud.nurse.dborm.MonitorKindDao;
import com.hxkcloud.nurse.request.MessageCode;
import com.hxkcloud.nurse.request.YiJiaHuNetWorkUtils;
import com.hxkcloud.nurse.utils.ImageUtil;
import com.hxkcloud.nurse.utils.StringUtil;
import com.hxkcloud.nurse.views.CustomWareView;
import com.hxkcloud.nurse.views.SuperGridView;
import com.hxkcloud.nurse.views.WaveHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ray_lei on 2016/7/28.
 * implements View.OnClickListener, Chart1Adapter.ResultSelectedListener, PullToRefreshLayout.OnRefreshListener
 */
public class MonitorChart1Activity extends BaseActivity implements View.OnClickListener{
//iv_moitor_title_back
    private LinearLayout ll_titles;
    private ImageView iv_back;
    private TextView tv_titles;
    private TextView tv_showHint;
//    private Timer timer;
//    private PullToRefreshLayout refreshLayout;
//    private PullableRecyclerView recycleView;
//
    private List<LineListBean> chartInfoList;
   // private List<BloodListBean> chartBloodList;
//    private Chart1Adapter adapter;

    private String patientId = "";

    private ObjectResultBean<MyChartListBean> resultBean;

    private boolean isFromList = false;

    //修改客户端
    private View view;
    private LinearLayout ll_title;
    private TextView tv_title;
    private ImageView iv_message;
    //小红点
    //private ImageView iv_msgpoint;
    private ImageView iv_logo;
    private LinearLayout ll_back;
    private View view_xueya;
    private View view_xuetang;
    private View view_xuezhi;
    private View view_drug;
    private View view_report;
    private List<MonitorKindBean> kinds;
    private MonitorKindBean hasXueya = null;
    private MonitorKindBean hasXuetang = null;
    private MonitorKindBean hasXuezhi = null;
    private TextView msg_number;
    private SuperGridView gridview_monitorpatient;
    private MonitorPatientAdapter Moadapter;
    private List<MonitorAdapterBean> monitorAdapterBeanList;
    private String[] titleContent=new String[]{"血压","血糖","血脂","报告","服药","出院"};
    private String[] titleDes=new String[]{"监测血压","监测血糖","监测血脂","检查报告","服药记录","出院小结"};
    private String[] itemColor=new String[]{"#0b87f4","#bb8dff","#f8796a","#fdda74","#44c9d6","#9edd91"};
    private CustomWareView wave_water;

    private WaveHelper waveHelper;

    private MonitorKindDao monitorKindDao;
    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.NETWORKCODE_GETMONITORDAYDATA_SUCCESS:
                dismisssLoading();
                if (msg.obj != null) {
                    resultBean = (ObjectResultBean<MyChartListBean>) msg.obj;
                    if (resultBean != null) {
                        chartInfoList = resultBean.getData().getChartList();
                     //   Log.i("-----chartInfoList---->",chartInfoList.toString());

//                        adapter.setData(chartInfoList);
//                        toastMsg(resultBean.getMessage());
                    }
                }
                break;
            case MessageCode.NETWORKCODE_GETMONITORDAYDATA_FAILURE:
                dismisssLoading();
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                    return;
                }
                break;

        }

    }

//    private void isMonitorReady() {
//        //获取数据客户端
//        monitorKindDao = new MonitorKindDao(mContext);
//        for (int i=0;i<chartInfoList.size();i++){
//           kinds.add( monitorKindDao.getKind(chartInfoList.get(i).getKindId()));
//        }
//       // kinds = monitorKindDao.getKinds();
//        for (MonitorKindBean kind : kinds) {
//            if (kind.getName().contains("血压")) {
//                hasXueya = kind;
//            }
//            if (kind.getName().contains("血糖")) {
//                hasXuetang = kind;
//            }
//            if (kind.getName().contains("血脂")) {
//                hasXuezhi = kind;
//            }
//        }
//
//    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //R.layout.activity_layout_chart1
        setContentView(R.layout.activity_layout_monitorchart1);

        if (getIntent().hasExtra("patientId")) {
            patientId = getIntent().getStringExtra("patientId");
        }
        if(getIntent().hasExtra("isFromList")){
            isFromList = getIntent().getBooleanExtra("isFromList",false);
        }
        if(null == patientId || "".equals(patientId))
        {
            patientId = savedInstanceState.getString("patientId");
        }


        chartInfoList = new ArrayList<LineListBean>();
        kinds = new ArrayList<MonitorKindBean>();
//        timer = new Timer();
        initView();
        initData();
      //  isMonitorReady();
        initListener();

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("patientId",patientId);

    }

    @Override
    protected void initView() {
        ll_titles = (LinearLayout) findViewById(R.id.ll_title);
        ll_titles.setBackgroundResource(R.color.alpha);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_titles = (TextView) findViewById(R.id.tv_title_title);
        tv_showHint  = (TextView) findViewById(R.id.monitorchart1_showtextview);
        setTitlePadding(ll_titles);
        iv_back.setOnClickListener(this);
        iv_back.setVisibility(View.VISIBLE);
        tv_titles.setText(getResources().getString(R.string.medical_record));





//        refreshLayout = (PullToRefreshLayout) findViewById(R.id.chart1_refreshlayout);
//        recycleView = (PullableRecyclerView) findViewById(R.id.chart1_recyclerview);
//
//        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
//        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//        recycleView.setLayoutManager(mLayoutManager);
//
//
//        adapter = new Chart1Adapter(mContext, chartInfoList, recycleView, this, patientId, loginInfo.getUserStyle().equals(YjhApplication.userstyle_patient), screenWidth);
//        recycleView.setAdapter(adapter);
    //修改的客户端



        wave_water = (CustomWareView) findViewById(R.id.wave_water);
        monitorAdapterBeanList =new ArrayList<>();
        gridview_monitorpatient = (SuperGridView) findViewById(R.id.gridview_monitorpatient);
        gridview_monitorpatient.setNumColumns(2);
        msg_number = (TextView) findViewById(R.id.msg_number);
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        tv_title = (TextView) findViewById(R.id.tv_title_title);
        iv_message = (ImageView) findViewById(R.id.iv_title_func);
        //iv_msgpoint = (ImageView) view.findViewById(R.id.iv_title_point);

        setTitlePadding(ll_title);
        //  tv_title.setText(getResources().getText(R.string.medical_record));
        iv_message.setVisibility(View.GONE);
        iv_message.setImageResource(R.mipmap.nav_message);
        iv_message.setOnClickListener(this);

        iv_logo = (ImageView) findViewById(R.id.iv_monitorpatient_logo);
        ll_back = (LinearLayout) findViewById(R.id.ll_monitorpatient_back);
        view_xueya = findViewById(R.id.view_monitorpatient_xueya);
        view_xuetang = findViewById(R.id.view_monitorpatient_xuetang);
        view_xuezhi = findViewById(R.id.view_monitorpatient_xuezhi);
        view_drug = findViewById(R.id.view_monitorpatient_drug);
        view_report = findViewById(R.id.view_monitorpatient_report);

        LinearLayout.LayoutParams lp_logo = new LinearLayout.LayoutParams(screenWidth / 3, screenWidth / 4);
        iv_logo.setLayoutParams(lp_logo);
        int topMargin = ImageUtil.dip2px(20, mContext);
        int leftMargin = ImageUtil.dip2px(12, mContext);
        LinearLayout.LayoutParams lp_back = new LinearLayout.LayoutParams(screenWidth - leftMargin - leftMargin, (screenWidth - leftMargin - leftMargin) * 2 / 3);
        lp_back.setMargins(leftMargin, topMargin, leftMargin, topMargin);
        ll_back.setLayoutParams(lp_back);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        waveHelper.cancel();
       // timer.cancel();
    }

    @Override
    protected void initListener() {
        //refreshLayout.setOnRefreshListener(this);
    }
//    Handler timerHandler = new Handler(){
//        @Override
//        public void handleMessage(Message msg) {
//            tv_showHint.setVisibility(View.GONE);
//        }
//    };
    @Override
    protected void initData() {
        loading();
        tv_showHint.setVisibility(View.GONE);
        //
       YiJiaHuNetWorkUtils.getMonitorDayData(patientId, handler);
//        //判断是否是护士
//        loginInfo.getUserStyle().equals(YjhApplication.userstyle_patient);
//        timer.schedule(new TimerTask() {
//            @Override
//            public void run() {
//                timerHandler.sendEmptyMessage(1);
//            }
//        },10000);
        //获取客户端的数据
        for (int i = 0; i < titleContent.length; i++) {
            MonitorAdapterBean monitorAdapterBean=new MonitorAdapterBean();
            monitorAdapterBean.setItemColor(itemColor[i]);
            monitorAdapterBean.setTitleContent(titleContent[i]);
            monitorAdapterBean.setTitleDes(titleDes[i]);
            monitorAdapterBeanList.add(monitorAdapterBean);
        }
        Moadapter=new MonitorPatientAdapter(mContext,monitorAdapterBeanList);
        gridview_monitorpatient.setAdapter(Moadapter);

        gridview_monitorpatient.setOnItemClickListener(new SuperGridView.OnItemClickListener() {
            @Override
            public void onItemClick(LinearLayout parent, View view, int position) {
                switch (position){
                    case 0://血压
                        //if (!TextUtils.isEmpty(loginInfo.getDepartmentId())) {
                           // if (hasXueya != null) {
                                StringUtil.zhugeTrack(mContext, "监测-" + chartInfoList.get(position).getName(), "监测中" + chartInfoList.get(position).getName() + "区域点击数");
                                Intent intent = new Intent(mContext, ChartDetailActivity.class);
                                intent.putExtra("isFromList", true);
                                intent.putExtra("patientId", patientId);
                                intent.putExtra("kindId", chartInfoList.get(position).getKindId());
                                intent.putExtra("name", chartInfoList.get(position).getName());
                                intent.putExtra("time", chartInfoList.get(position).getDate());
                                intent.putExtra("min", chartInfoList.get(position).getMinValue());
                                intent.putExtra("max", chartInfoList.get(position).getMaxValue());
                                Log.i("----chartInfoList---->",chartInfoList.get(position).toString());
                                if (chartInfoList.get(position).getLineList() != null) {
                                    for (int i = 0; i < chartInfoList.get(position).getLineList().size(); i++) {
                                        intent.putExtra("text" + i, chartInfoList.get(position).getLineList().get(i).getName());
                                    }
                                }
                                startActivityForResult(intent,111);

//                                //StringUtil.zhugeTrack(mContext, "血压" + "录入保存按钮", "血压" + "录入保存按钮点击数");
//                                Intent intent = new Intent(mContext, ChartDetailActivity.class);
//                                intent.putExtra("patientId", loginInfo.getPatientId());
//                                intent.putExtra("kindId", hasXueya.getKindId());
//                                intent.putExtra("name", hasXueya.getName());
//                                intent.putExtra("min", hasXueya.getMinValue());
//                                intent.putExtra("max", hasXueya.getMaxValue());
//                                if (hasXueya.getValueList() != null && hasXueya.getValueList().size() > 0) {
//                                    for (int i = 0; i < hasXueya.getValueList().size(); i++) {
//                                        intent.putExtra("text" + i, hasXueya.getValueList().get(i).getName());
//                                    }
//                                } else {
//                                    intent.putExtra("text" + 0, hasXueya.getName());
//                                }
//                                startActivity(intent);
//                            } else {
//                                toastMsg(R.string.departmenthavenotthismonitor);
//                            }
//                        } else {
//                            toastMsg(R.string.pleasefirstjoindepartment);
                       // }
                        break;
                    case 1://血糖
                       // if (!TextUtils.isEmpty(loginInfo.getDepartmentId())) {
                          //  if (hasXuetang != null) {
                                StringUtil.zhugeTrack(mContext, "监测-" + chartInfoList.get(1).getName(), "监测中" + chartInfoList.get(1).getName() + "区域点击数");
                                Intent intent1 = new Intent(mContext, ChartDetailActivity.class);
                                intent1.putExtra("isFromList", true);
                                intent1.putExtra("patientId", patientId);
                                intent1.putExtra("kindId", chartInfoList.get(1).getKindId());
                                intent1.putExtra("name", chartInfoList.get(1).getName());
                                intent1.putExtra("time", chartInfoList.get(1).getDate());
                                intent1.putExtra("min", chartInfoList.get(1).getMinValue());
                                intent1.putExtra("max", chartInfoList.get(1).getMaxValue());
                                if (chartInfoList.get(1).getLineList() != null) {
                                    for (int i = 0; i < chartInfoList.get(1).getLineList().size(); i++) {
                                        intent1.putExtra("text" + i, chartInfoList.get(1).getLineList().get(i).getName());
                                    }
                                }
                                startActivityForResult(intent1,111);
//                                //  StringUtil.zhugeTrack(mContext, "监测-" + chartInfoList.get(position).getName(), "监测中" + chartInfoList.get(position).getName() + "区域点击数");
//                                StringUtil.zhugeTrack(mContext, "血糖" + "录入保存按钮", "血糖" + "录入保存按钮点击数");
//
//                                Intent intent = new Intent(mContext, ChartDetailActivity.class);
//                                intent.putExtra("patientId", loginInfo.getPatientId());
//                                intent.putExtra("kindId", hasXuetang.getKindId());
//                                intent.putExtra("name", hasXuetang.getName());
//                                intent.putExtra("min", hasXuetang.getMinValue());
//                                intent.putExtra("max", hasXuetang.getMaxValue());
//                                if (hasXuetang.getValueList() != null && hasXuetang.getValueList().size() > 0) {
//                                    for (int i = 0; i < hasXuetang.getValueList().size(); i++) {
//                                        intent.putExtra("text" + i, hasXuetang.getValueList().get(i).getName());
//                                    }
//                                } else {
//                                    intent.putExtra("text" + 0, hasXuetang.getName());
//                                }
//                                startActivity(intent);
//                            } else {
//                                toastMsg(R.string.departmenthavenotthismonitor);
//                            }
//                        } else {
//                            toastMsg(R.string.pleasefirstjoindepartment);
                      //  }
                        break;
                    case 2://血脂
                        //if (hasXuezhi != null) {
                            StringUtil.zhugeTrack(mContext, "监测-" + chartInfoList.get(2).getName(), "监测中" + chartInfoList.get(2).getName() + "区域点击数");
                            Intent intent2 = new Intent(mContext, ChartDetailActivity.class);
                            intent2.putExtra("isFromList", true);
                            intent2.putExtra("patientId", patientId);
                            intent2.putExtra("kindId", chartInfoList.get(2).getKindId());
                            intent2.putExtra("name", chartInfoList.get(2).getName());
                            intent2.putExtra("time", chartInfoList.get(2).getDate());
                            intent2.putExtra("min", chartInfoList.get(2).getMinValue());
                            intent2.putExtra("max", chartInfoList.get(2).getMaxValue());
                            if (chartInfoList.get(2).getLineList() != null) {
                                for (int i = 0; i < chartInfoList.get(2).getLineList().size(); i++) {
                                    intent2.putExtra("text" + i, chartInfoList.get(2).getLineList().get(i).getName());
                                }
                            }
                            startActivityForResult(intent2, 111);
                     //   }
//                        if (!TextUtils.isEmpty(loginInfo.getDepartmentId())) {
//                            if (hasXuezhi != null) {
//                                StringUtil.zhugeTrack(mContext, "血脂" + "录入保存按钮", "血脂" + "录入保存按钮点击数");
//                              //  StringUtil.zhugeTrack(mContext, "监测-" + chartInfoList.get(position).getName(), "监测中" + chartInfoList.get(position).getName() + "区域点击数");
//                                Intent intent = new Intent(mContext, ChartDetailActivity.class);
//                                intent.putExtra("patientId", loginInfo.getPatientId());
//                                intent.putExtra("kindId", hasXuezhi.getKindId());
//                                intent.putExtra("name", hasXuezhi.getName());
//                                intent.putExtra("min", hasXuezhi.getMinValue());
//                                intent.putExtra("max", hasXuezhi.getMaxValue());
//                                if (hasXuezhi.getValueList() != null && hasXuezhi.getValueList().size() > 0) {
//                                    for (int i = 0; i < hasXuezhi.getValueList().size(); i++) {
//                                        intent.putExtra("text" + i, hasXuezhi.getValueList().get(i).getName());
//                                    }
//                                } else {
//                                    intent.putExtra("text" + 0, hasXuezhi.getName());
//                                }
//                                startActivity(intent);
//                            } else {
//                                toastMsg(R.string.departmenthavenotthismonitor);
//                            }
//                        } else {
//                            toastMsg(R.string.pleasefirstjoindepartment);
//                        }
                        break;
                    case 3://报告
                        Intent certIntent = new Intent(mContext, MyReportExpandListActivity.class);
                        certIntent.putExtra("patientId",patientId);

                        startActivity(certIntent);
                        break;
                    case 4://服药
                        Intent drugsIntent = new Intent(mContext, TakeDrugsActivity.class);
                        drugsIntent.putExtra("patientId",patientId);

                        startActivity(drugsIntent);
                        break;
                    case 5://出院
//                        Intent dischargeIntent = new Intent(getActivity(), DischargeSummaryActivity.class);
//                        startActivity(dischargeIntent);
                        Intent dischargeIntent =new Intent(MonitorChart1Activity.this,DischargeSummaryActivity.class);
                        dischargeIntent.putExtra("patientId",patientId);
                        startActivity(dischargeIntent);
                        break;
                }
            }
        });


        waveHelper = new WaveHelper(wave_water);
        wave_water.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (!waveHelper.isStartAnimator())
                    waveHelper.start();
            }
        },300);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back:
                finish();
                break;

        }
    }

//
//    @Override
//    public void onSelected(int position, String type) {
//        if ("itemClick".equals(type)) {
//            StringUtil.zhugeTrack(mContext, "监测-" + chartInfoList.get(position).getName(), "监测中" + chartInfoList.get(position).getName() + "区域点击数");
//
//            Intent intent = new Intent(mContext, ChartDetailActivity.class);
//            intent.putExtra("isFromList", true);
//            intent.putExtra("patientId", patientId);
//            intent.putExtra("kindId", chartInfoList.get(position).getKindId());
//            intent.putExtra("name", chartInfoList.get(position).getName());
//            intent.putExtra("time", chartInfoList.get(position).getDate());
//            intent.putExtra("min", chartInfoList.get(position).getMinValue());
//            intent.putExtra("max", chartInfoList.get(position).getMaxValue());
//            if (chartInfoList.get(position).getLineList() != null) {
//                for (int i = 0; i < chartInfoList.get(position).getLineList().size(); i++) {
//                    intent.putExtra("text" + i, chartInfoList.get(position).getLineList().get(i).getName());
//                }
//            }
//            startActivityForResult(intent, 111);
//        }
//
//
//    }
//
//    @Override
//    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
//        refreshLayout
//                .refreshFinish(PullToRefreshLayout.SUCCEED);
//    }
//
//    @Override
//    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
//        refreshLayout
//                .loadmoreFinish(PullToRefreshLayout.SUCCEED);
//    }
}
